package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWithdrawLog;
import com.zb.mapper.ZjWithdrawLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjWithdrawLogService;
import com.zb.vo.WithdrawLogListVo;
import com.zb.vo.WithdrawLogVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 提现操作日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-27
 */
@Log4j2
@Service
public class ZjWithdrawLogServiceImpl extends ServiceImpl<ZjWithdrawLogMapper, ZjWithdrawLog> implements ZjWithdrawLogService {

    // 保存提现日志
    @Override
    public void save(Integer applyEmpType, WithdrawLogVo withdrawLogVo) {
        if (null == withdrawLogVo) {
            return;
        }
        List<WithdrawLogVo> withdrawLogVos = new ArrayList<>();
        withdrawLogVos.add(withdrawLogVo);
        this.save(applyEmpType, withdrawLogVos);
    }

    // 保存提现日志
    @Override
    public void save(Integer applyEmpType, List<WithdrawLogVo> withdrawLogVos) {
        if (CollectionUtil.isEmpty(withdrawLogVos)) {
            return;
        }
        try {
            List<ZjWithdrawLog> logs = new ArrayList<>();
            ZjWithdrawLog log = null;
            for (WithdrawLogVo vo : withdrawLogVos) {
                log = new ZjWithdrawLog();
                BeanUtil.copyProperties(vo, log);
                log.setApplyEmpType(applyEmpType);
                logs.add(log);
            }
            this.saveBatch(logs);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("添加提现日志出错：{}", e.getMessage());
        }
    }

    // 根据提现单ID获取操作日志
    @Override
    public Map<String, Object> getWithdrawLogList(Long withdrawId, PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        QueryWrapper<ZjWithdrawLog> wrapper = new QueryWrapper<>();
        wrapper.select("id", "apply_emp", "network", "event", "remark", "gmt_create")
                .eq("withdraw_id", withdrawId)
                .orderByDesc("id");
        Page<ZjWithdrawLog> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(log -> {
            WithdrawLogListVo vo = new WithdrawLogListVo();
            BeanUtil.copyProperties(log, vo);
            vo.setCreateTime(log.getGmtCreate());
            return vo;
        }).toList());
    }
}
